﻿using System;
using ZX.Common;
using ZX.DBModule;
using ZX.Web.UI.Controls;
using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using ZX.Common.Extend;
using ZX.EHR.Library;

namespace ZX.WebApp.TR
{
    public partial class DatePlan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.WebApp.TR.DatePlan));
            string wpid = Request.QueryString["DPID"];
            string type = Request.QueryString["Type"];
            if (!this.IsPostBack)
            {
                Redirect(wpid, type);
            }
        }

        protected void Redirect(string wpid, string type)
        {
            DatUser du = (DatUser)Session["User"];
            if (wpid == "" && type == "P")
            {
                LabDateP.Text = DateTime.Now.ToShortDateString();
                LabDateR.Text = DateTime.Now.AddDays(-1).ToShortDateString();
                LabName.Text = du.UName;
                ZX.TR.Library.TrDatePlan tdp = new ZX.TR.Library.TrDatePlan();
                BaseBussnies bbp = new BaseBussnies(tdp);
                IList<ZX.TR.Library.V_TrDatePlan> tdpl = bbp.GetEntitys<ZX.TR.Library.V_TrDatePlan>("WriteDate='" + DateTime.Now.ToShortDateString() + "' AND Adduser='"+du.UserID+"'");
                if (tdpl != null)
                {
                    bangdata(tdpl[0].TrDatePlanID.ToString(),type);
                }
                else
                {
                    ZX.TR.Library.TrDatePlan tdr = new ZX.TR.Library.TrDatePlan();
                    BaseBussnies bbr = new BaseBussnies(tdr);
                    IList<ZX.TR.Library.V_TrDatePlan> tdrl = bbr.GetEntitys<ZX.TR.Library.V_TrDatePlan>("WriteDate='" + LabDateR.Text + "' AND AddUser='" + du.UserID + "'");
                    if (tdrl != null)
                    {
                        hdfdrid.Value = tdrl[0].TrDatePlanID.ToString();
                        bangWork(Guid.NewGuid(), tdrl[0].TrDatePlanID);
                    }
                }
            }
            else
            {
                bangdata(wpid, type);
            }
        }

        protected void bangdata(string wpid,string type)
        {
            
            if (wpid != "" && type == "P")
            {
                hdfdpid.Value = wpid;
                ZX.TR.Library.TrDatePlan tdp = new ZX.TR.Library.TrDatePlan();
                ZX.TR.Library.TrDatePlan tdr = new ZX.TR.Library.TrDatePlan();
                BaseBussnies bbp = new BaseBussnies(tdp);
                BaseBussnies bbr = new BaseBussnies(tdr);
                IList<ZX.TR.Library.V_TrDatePlan> tdpl = bbp.GetEntitys<ZX.TR.Library.V_TrDatePlan>("TrDatePlanID='" + wpid + "'");
                if (tdpl != null)
                {
                    LabDateR.Text = tdpl[0].WriteDate.AddDays(-1).ToShortDateString();
                    LabDateP.Text = tdpl[0].WriteDate.ToShortDateString();
                    LabName.Text = tdpl[0].UName;
                    TaDateShare.Value = tdpl[0].DateShare;
                    TaWarpMend.Value = tdpl[0].WarpMend;
                    TaSelfUp.Value = tdpl[0].SelfUp;
                    TaNowPoint.Value = tdpl[0].NowPoint;

                    IList<ZX.TR.Library.V_TrDatePlan> tdrl = bbr.GetEntitys<ZX.TR.Library.V_TrDatePlan>("WriteDate='" + LabDateR.Text + "' AND AddUser='" + tdpl[0].AddUser + "'");
                    if (tdrl != null)
                    {
                        hdfdrid.Value = tdrl[0].TrDatePlanID.ToString();
                        bangWork(new Guid(wpid), tdrl[0].TrDatePlanID);
                    }
                    else
                    {
                        bangWork(new Guid(wpid), Guid.NewGuid());
                    }
                    
                }
                

            }
            else if (wpid != "" && type == "R")
            {
                hdfdrid.Value = wpid;
                ZX.TR.Library.TrDatePlan tdp = new ZX.TR.Library.TrDatePlan();
                ZX.TR.Library.TrDatePlan tdr = new ZX.TR.Library.TrDatePlan();
                BaseBussnies bbp = new BaseBussnies(tdp);
                BaseBussnies bbr = new BaseBussnies(tdr);
                IList<ZX.TR.Library.V_TrDatePlan> tdpl = bbp.GetEntitys<ZX.TR.Library.V_TrDatePlan>("TrDatePlanID='" + wpid + "'");
                if (tdpl != null)
                {
                    LabDateR.Text = tdpl[0].WriteDate.ToShortDateString();
                    LabDateP.Text = tdpl[0].WriteDate.AddDays(1).ToShortDateString();
                    LabName.Text = tdpl[0].UName;
                    TaDateShare.Value = tdpl[0].DateShare;
                    TaWarpMend.Value = tdpl[0].WarpMend;
                    TaSelfUp.Value = tdpl[0].SelfUp;
                    TaNowPoint.Value = tdpl[0].NowPoint;
                    IList<ZX.TR.Library.V_TrDatePlan> tdrl = bbr.GetEntitys<ZX.TR.Library.V_TrDatePlan>("WriteDate='" + LabDateP.Text + "' AND AddUser='" + tdpl[0].AddUser + "'");
                    if (tdrl != null)
                    {
                        hdfdpid.Value = tdrl[0].TrDatePlanID.ToString();
                        bangWork(tdrl[0].TrDatePlanID, new Guid(wpid));
                    }
                    else
                    {
                        bangWork(Guid.NewGuid(), new Guid(wpid));
                    }
                    
                }

                
                
            }
        }

        //绑定工作项
        protected void bangWork(Guid gp,Guid gr)
        {
            ZX.TR.Library.TrDateWork tdw = new ZX.TR.Library.TrDateWork();
            BaseBussnies bbw = new BaseBussnies(tdw);
            IList<ZX.TR.Library.V_TrDateWork> tdwlr = bbw.GetEntitys<ZX.TR.Library.V_TrDateWork>("TrDatePlanID='" + gr + "'");
            IList<ZX.TR.Library.V_TrDateWork> tdwlp = bbw.GetEntitys<ZX.TR.Library.V_TrDateWork>("TrDatePlanID='" + gp + "'");
            if (tdwlr != null)
            {
                if (tdwlr[0].WorkType == "S")
                {
                    hdfdrw1.Value = tdwlr[0].TrDateWorkID.ToString();
                    Ta11.Value = tdwlr[0].WorkContent;
                    Ta12.Value = tdwlr[0].InfactResult;
                    Ta13.Value = tdwlr[0].DoneCase;
                    Ta14.Value = tdwlr[0].SelfCase;

                    hdfdrw2.Value = tdwlr[1].TrDateWorkID.ToString();
                    Ta21.Value = tdwlr[1].WorkContent;
                    Ta22.Value = tdwlr[1].InfactResult;
                    Ta23.Value = tdwlr[1].DoneCase;
                    Ta24.Value = tdwlr[1].SelfCase;
                }
                else
                {
                    hdfdrw1.Value = tdwlr[1].TrDateWorkID.ToString();
                    Ta11.Value = tdwlr[1].WorkContent;
                    Ta12.Value = tdwlr[1].InfactResult;
                    Ta13.Value = tdwlr[1].DoneCase;
                    Ta14.Value = tdwlr[1].SelfCase;

                    hdfdrw2.Value = tdwlr[0].TrDateWorkID.ToString();
                    Ta21.Value = tdwlr[0].WorkContent;
                    Ta22.Value = tdwlr[0].InfactResult;
                    Ta23.Value = tdwlr[0].DoneCase;
                    Ta24.Value = tdwlr[0].SelfCase;
                }
            }

            if (tdwlp != null)
            {
                if (tdwlp[0].WorkType == "S")
                {
                    hdfdpw1.Value = tdwlp[0].TrDateWorkID.ToString();
                    Ta31.Value = tdwlp[0].WorkContent;
                    Ta32.Value = tdwlp[0].PreResult;
                    //hdf33.Value = tdwlp[0].Checker.ToString();
                    //Ta33.Value = tdwlp[0].checkername.ToString();
                    Ta34.Value = tdwlp[0].PlanSelf;

                    hdfdpw2.Value = tdwlp[1].TrDateWorkID.ToString();
                    Ta41.Value = tdwlp[1].WorkContent;
                    Ta42.Value = tdwlp[1].PreResult;
                    //hdf43.Value = tdwlp[1].Checker.ToString();
                    //Ta43.Value = tdwlp[1].checkername.ToString();
                    Ta44.Value = tdwlp[1].PlanSelf;
                }
                else
                {
                    hdfdpw1.Value = tdwlp[1].TrDateWorkID.ToString();
                    Ta31.Value = tdwlp[1].WorkContent;
                    Ta32.Value = tdwlp[1].PreResult;
                    //hdf33.Value = tdwlp[1].Checker.ToString();
                    //Ta33.Value = tdwlp[1].checkername.ToString();
                    Ta34.Value = tdwlp[1].PlanSelf;

                    hdfdpw2.Value = tdwlp[0].TrDateWorkID.ToString();
                    Ta41.Value = tdwlp[0].WorkContent;
                    Ta42.Value = tdwlp[0].PreResult;
                    //hdf43.Value = tdwlp[0].Checker.ToString();
                    //Ta43.Value = tdwlp[0].checkername.ToString();
                    Ta44.Value = tdwlp[0].PlanSelf;
                }
            }

        }
        protected void IbtnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                #region 主表信息
                string type = Request.QueryString["Type"];
                string drid = hdfdrid.Value;
                ZX.TR.Library.TrDatePlan tdr = new ZX.TR.Library.TrDatePlan();
                //日结果(需要先建计划，才能建结果)
                if (drid != null && drid != "")
                {
                    BaseBussnies bbp = new BaseBussnies(tdr);
                    IList<ZX.TR.Library.TrDatePlan> tdrl = bbp.GetEntitys<ZX.TR.Library.TrDatePlan>("TrDatePlanID='" + drid + "'");
                    if (tdrl != null && type == "R")
                    {
                        tdrl[0].NowPoint = TaNowPoint.Value;
                        tdrl[0].WarpMend = TaWarpMend.Value;
                        tdrl[0].DateShare = TaDateShare.Value;
                        tdrl[0].SelfUp = TaSelfUp.Value;
                        BaseBussnies bbr = new BaseBussnies(tdrl[0]);
                        bbr.Update();
                    }
                }

                ZX.TR.Library.TrDatePlan tdp = new ZX.TR.Library.TrDatePlan();
                
                string dpid = hdfdpid.Value;
                
                DatUser du = (DatUser)Session["User"];
                //日计划
                if (dpid == null || dpid == "")
                {
                    tdp.TrDatePlanID = Guid.NewGuid();
                    tdp.AddUser = du.UserID;
                    tdp.AddTime = DateTime.Now;
                    tdp.WriteDate = Convert.ToDateTime(LabDateP.Text);
                    if (type == "P")
                    {
                        tdp.NowPoint = TaNowPoint.Value;
                        tdp.WarpMend = TaWarpMend.Value;
                        tdp.DateShare = TaDateShare.Value;
                        tdp.SelfUp = TaSelfUp.Value;
                        
                    }
                    BaseBussnies bbp = new BaseBussnies(tdp);
                    bbp.Insert();
                    hdfdpid.Value = tdp.TrDatePlanID.ToString();
                }
                else
                {
                    BaseBussnies bbp = new BaseBussnies(tdp);
                    IList<ZX.TR.Library.TrDatePlan> tdpl = bbp.GetEntitys<ZX.TR.Library.TrDatePlan>("TrDatePlanID='" + dpid + "'");
                    if (tdpl != null && type == "P")
                    {
                        tdpl[0].NowPoint = TaNowPoint.Value;
                        tdpl[0].WarpMend = TaWarpMend.Value;
                        tdpl[0].DateShare = TaDateShare.Value;
                        tdpl[0].SelfUp = TaSelfUp.Value;
                        BaseBussnies bbu = new BaseBussnies(tdpl[0]);
                        bbu.Update();
                    }
                }
                #endregion

                #region 从表
                ZX.TR.Library.TrDateWork tdpw1 = new ZX.TR.Library.TrDateWork();
                ZX.TR.Library.TrDateWork tdpw2 = new ZX.TR.Library.TrDateWork();
                ZX.TR.Library.TrDateWork tdrw1 = new ZX.TR.Library.TrDateWork();
                ZX.TR.Library.TrDateWork tdrw2 = new ZX.TR.Library.TrDateWork();
                string dpw1 = hdfdpw1.Value;
                string dpw2 = hdfdpw2.Value;
                string drw1 = hdfdrw1.Value;
                string drw2 = hdfdrw2.Value;
                //计划作项1
                if (dpw1 == null || dpw1 == "")
                {
                    tdpw1.TrDateWorkID = Guid.NewGuid();
                    tdpw1.TrDatePlanID = new Guid(hdfdpid.Value);
                    tdpw1.WorkType = "S";
                    tdpw1.WorkContent = Ta31.Value;
                    tdpw1.PreResult = Ta32.Value;
                    //if (hdf33.Value != "" && hdf33.Value != "undefined")
                    //    tdpw1.Checker = new Guid(hdf33.Value);
                    tdpw1.PlanSelf = Ta34.Value;
                    BaseBussnies bbW1 = new BaseBussnies(tdpw1);
                    bbW1.Insert();
                }
                else
                {
                    BaseBussnies b1 = new BaseBussnies(tdpw1);
                    IList<ZX.TR.Library.TrDateWork> tdw1 = b1.GetEntitys<ZX.TR.Library.TrDateWork>("TrDateWorkID='" + dpw1 + "'");
                    if (tdw1 != null)
                    {
                        tdw1[0].WorkContent = Ta31.Value;
                        tdw1[0].PreResult = Ta32.Value;
                        //if (hdf33.Value != "" && hdf33.Value != "undefined")
                        //    tdw1[0].Checker = new Guid(hdf33.Value);
                        tdw1[0].PlanSelf = Ta34.Value;
                        BaseBussnies b11 = new BaseBussnies(tdw1[0]);
                        b11.Update();
                    }
                }
                //计划作项2
                if (dpw2 == null || dpw2 == "")
                {
                    tdpw2.TrDateWorkID = Guid.NewGuid();
                    tdpw2.TrDatePlanID = new Guid(hdfdpid.Value);
                    tdpw2.WorkType = "X";
                    tdpw2.WorkContent = Ta41.Value;
                    tdpw2.PreResult = Ta42.Value;
                    //if (hdf43.Value != "" && hdf43.Value != "undefined")
                    //    tdpw2.Checker = new Guid(hdf43.Value);
                    tdpw2.PlanSelf = Ta44.Value;
                    BaseBussnies bbW2 = new BaseBussnies(tdpw2);
                    bbW2.Insert();
                }
                else
                {
                    BaseBussnies b2 = new BaseBussnies(tdpw2);
                    IList<ZX.TR.Library.TrDateWork> tdw2 = b2.GetEntitys<ZX.TR.Library.TrDateWork>("TrDateWorkID='" + dpw2 + "'");
                    if (tdw2 != null)
                    {
                        tdw2[0].WorkContent = Ta41.Value;
                        tdw2[0].PreResult = Ta42.Value;
                        //if (hdf43.Value != "" && hdf43.Value != "undefined")
                        //    tdw2[0].Checker = new Guid(hdf43.Value);
                        tdw2[0].PlanSelf = Ta44.Value;
                        BaseBussnies b22 = new BaseBussnies(tdw2[0]);
                        b22.Update();
                    }
                }

                //日结果父ID不存在，则不插入工作项
                if (hdfdrid.Value != "")
                {
                    //结果工作项1
                    if (drw1 == null || drw1 == "")
                    {
                        tdrw1.TrDateWorkID = Guid.NewGuid();
                        tdrw1.TrDatePlanID = new Guid(hdfdrid.Value);
                        tdrw1.WorkType = "S";
                        tdrw1.InfactResult = Ta12.Value;
                        tdrw1.DoneCase = Ta13.Value;
                        tdrw1.SelfCase = Ta14.Value;
                        BaseBussnies bbW3 = new BaseBussnies(tdrw1);
                        bbW3.Insert();
                    }
                    else
                    {
                        BaseBussnies b3 = new BaseBussnies(tdrw1);
                        IList<ZX.TR.Library.TrDateWork> drw11 = b3.GetEntitys<ZX.TR.Library.TrDateWork>("TrDateWorkID='" + drw1 + "'");
                        if (drw11 != null)
                        {
                            drw11[0].InfactResult = Ta12.Value;
                            drw11[0].DoneCase = Ta13.Value;
                            drw11[0].SelfCase = Ta14.Value;
                            BaseBussnies b33 = new BaseBussnies(drw11[0]);
                            b33.Update();
                        }
                    }
                    //结果工作项2
                    if (drw2 == null || drw2 == "")
                    {
                        tdrw2.TrDateWorkID = Guid.NewGuid();
                        tdrw2.TrDatePlanID = new Guid(hdfdrid.Value);
                        tdrw2.WorkType = "X";
                        tdrw2.InfactResult = Ta22.Value;
                        tdrw2.DoneCase = Ta23.Value;
                        tdrw2.SelfCase = Ta24.Value;
                        BaseBussnies bbW4 = new BaseBussnies(tdrw2);
                        bbW4.Insert();
                    }
                    else
                    {
                        BaseBussnies b4 = new BaseBussnies(tdrw2);
                        IList<ZX.TR.Library.TrDateWork> drw22 = b4.GetEntitys<ZX.TR.Library.TrDateWork>("TrDateWorkID='" + drw2 + "'");
                        if (drw22 != null)
                        {
                            drw22[0].InfactResult = Ta22.Value;
                            drw22[0].DoneCase = Ta23.Value;
                            drw22[0].SelfCase = Ta24.Value;
                            BaseBussnies b44 = new BaseBussnies(drw22[0]);
                            b44.Update();
                        }
                    }
                }
                #endregion

                Page.ClientScript.RegisterStartupScript(this.GetType(), "Alerteview", "AlertMsg(1)", true);
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Alerteview", "AlertMsg(0)", true);
            }
            
        }

        //检查人检测
        [AjaxPro.AjaxMethod]
        public string CheckUser(string users)
        {
            string[] us = users.Split(';');
            ZX.EHR.Library.DatUser du = new ZX.EHR.Library.DatUser();
            BaseBussnies bdu = new BaseBussnies(du);
            StringBuilder sbuid = new StringBuilder();

            foreach (string user in us)
            {
                if (user == "")
                    continue;
                //IList<ZX.EHR.Library.DatUser> dul = bdu.GetEntitys<ZX.EHR.Library.DatUser>("MessageType='公告' and IsPublic='是'");
                IList<ZX.EHR.Library.DatUser> dul = bdu.GetEntitys<ZX.EHR.Library.DatUser>("UName='" + user + "'");
                if (dul == null)//用户不存在
                {
                    sbuid.Append("检查人[" + user + "]不存在，请重新填写;");
                }
                else if (dul.Count > 1)//用户数大于1
                {
                    sbuid.Append("检查人[" + user + "]存在多个，请按【登录账号】填写;");
                }
                else
                {
                    sbuid.Append(dul[0].UserID + ";");
                }
            }
            return sbuid.ToString();
        }
    }
}